package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.ProdAuthCtrlMapper;
import cc.rengu.igas.bomp.common.entity.ProdAuthCtrl;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 王成 on 2020/3/6
 */
public class ProdAuthCtrlMapperImpl implements ProdAuthCtrlMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int inertProdAuthCtrlInfo(ProdAuthCtrl prodAuthCtrl) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "inertProdAuthCtrlInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, prodAuthCtrl, null);
        if (0 != iReturnCode) {
            rglog.error("登记产品业务权限控制信息失败,instId:<{}>,prodType:<{}>,baseProdCode:<{}>,txnNum:<{}>,bizType:<{}>,id:<{}>,iReturnCode:<{}>",
                    prodAuthCtrl.getInstId(), prodAuthCtrl.getProdType(), prodAuthCtrl.getBaseProdCode(), prodAuthCtrl.getTxnNum(), prodAuthCtrl.getBizType(), prodAuthCtrl.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public ProdAuthCtrl selectProdAuthCtrlInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectProdAuthCtrlInfoById");
        ProdAuthCtrl prodAuthCtrl = new ProdAuthCtrl();
        prodAuthCtrl.setInstId(instId);
        prodAuthCtrl.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, prodAuthCtrl, prodAuthCtrl);
        if (0 != iReturnCode) {
            rglog.error("获取产品业务权限控制信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return prodAuthCtrl;
    }

    @Override
    public int updateProdAuthCtrlInfoById(ProdAuthCtrl prodAuthCtrl) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateProdAuthCtrlInfoById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, prodAuthCtrl, null);
        if (0 != iReturnCode) {
            rglog.error("根据非业务主键更新产品业务权限控制信息失败,instId:<{}>,prodType:<{}>,baseProdCode:<{}>,txnNum:<{}>,bizType:<{}>,id:<{}>,iReturnCode:<{}>",
                    prodAuthCtrl.getInstId(), prodAuthCtrl.getProdType(), prodAuthCtrl.getBaseProdCode(), prodAuthCtrl.getTxnNum(), prodAuthCtrl.getBizType(), prodAuthCtrl.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public ProdAuthCtrl selectProdAuthCtrlInfoByPrimaryKey(String instId, String prodType, String baseProdCode, String txnNum, String bizType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectProdAuthCtrlInfoByPrimaryKey");
        ProdAuthCtrl prodAuthCtrl = new ProdAuthCtrl();
        prodAuthCtrl.setInstId(instId);
        prodAuthCtrl.setProdType(prodType);
        prodAuthCtrl.setBaseProdCode(baseProdCode);
        prodAuthCtrl.setTxnNum(txnNum);
        prodAuthCtrl.setBizType(bizType);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, prodAuthCtrl, prodAuthCtrl);
        if (0 != iReturnCode) {
            rglog.error("获取产品业务权限控制信息失败,instId:<{}>,prodType:<{}>,baseProdCode:<{}>,txnNum:<{}>,bizType:<{}>,iReturnCode:<{}>", instId, prodType, baseProdCode, txnNum, bizType, iReturnCode);
            return null;
        }
        return prodAuthCtrl;
    }

    @Override
    public List<ProdAuthCtrl> selectProdAuthCtrlInfoByBaseProdCode(String instId, String prodType, String baseProdCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectProdAuthCtrlInfoByBaseProdCode");
        ProdAuthCtrl prodAuthCtrl = new ProdAuthCtrl();
        prodAuthCtrl.setInstId(instId);
        prodAuthCtrl.setProdType(prodType);
        prodAuthCtrl.setBaseProdCode(baseProdCode);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, prodAuthCtrl, ProdAuthCtrl.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据控制主体获取业务权限控制信息失败,instId:<{}>,prodType:<{}>,baseProdCode:<{}>", instId, prodType, baseProdCode);
            return null;
        }
        return objectList.stream().map(item -> (ProdAuthCtrl) item).collect(Collectors.toList());
    }


    @Override
    public List<ProdAuthCtrl> selectProdAuthCtrlInfoByProdCode(String instId, String prodType, String baseProdCode, String prodCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompProdAuthCtrlInfoByProdCode");
        ProdAuthCtrl prodAuthCtrl = new ProdAuthCtrl();
        prodAuthCtrl.setInstId(instId);
        prodAuthCtrl.setProdType(prodType);
        prodAuthCtrl.setBaseProdCode(baseProdCode);
        prodAuthCtrl.setProdCode(prodCode);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, prodAuthCtrl, ProdAuthCtrl.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据控制主体获取业务权限控制信息失败,instId:<{}>,prodType:<{}>,baseProdCode:<{}>", instId, prodType, baseProdCode);
            return null;
        }
        return objectList.stream().map(item -> (ProdAuthCtrl) item).collect(Collectors.toList());
    }
}
